package cn.openea.perpartner.common.redis.config;

import io.micrometer.core.instrument.util.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * User:ysk<ysk@eventslack.com>
 * Date: 2023/5/29
 */

@Configuration
public class RedissonConfig {


    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private Integer database;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String url = "redis://" + host + ":" + port;
        SingleServerConfig serverConfig = config.useSingleServer().setAddress(url);
        if (StringUtils.isNotBlank(password)) {
            serverConfig.setPassword(password);
        }
        if (database != null) {
            serverConfig.setDatabase(database);
        }
        // 设置超时时间为10s
        serverConfig.setTimeout(10000);
        // 设置心跳检查 30秒检查一次
        serverConfig.setPingConnectionInterval(30000);
        return Redisson.create(config);
    }
}
